package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.SalaryBean;
import com.attendance.www.hr.beans.SalaryListBean;
import com.attendance.www.hr.beans.SalaryRequestBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
import com.attendance.www.hr.service.ISalaryRequestService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 工资报表申请
 * 2018-04-10
 * @author 张鑫
 *
 */
@Controller
@RequestMapping("/salaryRequest")
public class SalaryRequestController {
	
	@Resource
	private IClosedService closedService;
	@Resource
	private ISalaryRequestService salaryRequestService;
	
	/**
	 * 查询工资报表提交历史
	 * @param request
	 * @param salaryBean
	 * @return
	 */
	@RequestMapping(value = "/getSalaryRequestList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryRequestList(HttpServletRequest request,@RequestBody SalaryRequestBean salaryRequestBean) {
		if (salaryRequestBean.getPageCount()==null ||salaryRequestBean.getPageNo()==null
				||salaryRequestBean.getSelStartDate()==null
				||salaryRequestBean.getSelEndDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryRequestBean.setEmployeeId(employeeId);
		Integer startNum = 0;
		Integer pageNo = salaryRequestBean.getPageNo();
		Integer pageCount = salaryRequestBean.getPageCount();
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		salaryRequestBean.setStartNum(startNum);
		return ResultMap.success(salaryRequestService.getSalaryRequestList(salaryRequestBean));
	}
	
	/**
	 * 提交工资报表申请 (保存并提交按钮)
	 * @param request
	 * @param salaryRequestBean
	 * @return
	 */
	@RequestMapping(value = "/saveSalaryRequestList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveSalaryRequestList(HttpServletRequest request,@RequestBody SalaryListBean salaryListBean) {
		if (salaryListBean.getSalaryYear()==null
				||salaryListBean.getSalaryMonth()==null
				||(salaryListBean.getStartDate()==null && salaryListBean.getHistoryStartDate()==null)
				||(salaryListBean.getEndDate()==null && salaryListBean.getHistoryEndDate()==null)) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryListBean.setEmployeeId(employeeId);
		
		if (!closedService.verifyClosed(salaryListBean.getSalaryYear(), salaryListBean.getSalaryMonth())) {//封账验证
			return ResultMap.success(salaryRequestService.saveSalaryRequestList(salaryListBean));
		} else {
			throw new ResultFaultException("4_00003");
		}
	}
	
	/**
	 * by审批人查询申请信息
	 * @param request
	 * @param salaryRequestBean
	 * @return
	 */
	@RequestMapping(value = "/getSalaryRequestListByApl.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryRequestListByApl(HttpServletRequest request,@RequestBody SalaryRequestBean salaryRequestBean) {
		if (salaryRequestBean.getPageCount()==null ||salaryRequestBean.getPageNo()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryRequestBean.setEmployeeId(employeeId);
		Integer startNum = 0;
		Integer pageNo = salaryRequestBean.getPageNo();
		Integer pageCount = salaryRequestBean.getPageCount();
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		salaryRequestBean.setStartNum(startNum);
		return ResultMap.success(salaryRequestService.getSalaryRequestListByApl(salaryRequestBean));
	}
	
	/**
	 * 撤回申请
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getBackSalaryRequest.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getBackRequest(HttpServletRequest request){
		String requestIdStr=request.getParameter("requestId");
		if (StringUtils.isBlank(requestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer requestId=Integer.parseInt(requestIdStr);
		if (salaryRequestService.verifyGetBackRequest(requestId)) {
			throw new ResultFaultException("4_00019");
		}
		if (salaryRequestService.getBackSalaryRequest(requestId)) {
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00009");
		}
	}
	
	/**
	 * 删除申请
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteSalaryRequest.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object deleteSalaryRequest(HttpServletRequest request) {
		String requestIdStr=request.getParameter("requestId");
		if (StringUtils.isBlank(requestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer requestId=Integer.parseInt(requestIdStr);
		if (salaryRequestService.deleteSalaryRequest(requestId)) {
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00009");
		}
	}
	
	/**
	 * 获取报表年月
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getYearMonth.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object getYearMonth(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryBean.setEmployeeId(employeeId);
		return ResultMap.success(salaryRequestService.getYearMonth(salaryBean));
	}
	
	/**
	 * 撤回后再次提交，对应提交历史页"提交"按钮
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveSalaryRequestAgain.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object saveSalaryRequestAgain(HttpServletRequest request) {
		String requestIdStr=request.getParameter("requestId");
		if (StringUtils.isBlank(requestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		Integer requestId=Integer.parseInt(requestIdStr);
		if (salaryRequestService.saveSalaryRequestAgain(requestId)) {
			return ResultMap.success();
		}else{
			throw new ResultFaultException("0_00018");
		}
	}
}
